
clear all
global main "/Users/inga/Dropbox/Ais project/Repository"
cd "$main"

global data "$main/data_intermediate"
global input "$main/data_input"
global ps "/Users/inga/Documents/Projekte/Risk channel/Data/PSchott"



* PROJECT CONTAINER CHARGES ON DIRECT ROUTE CHARACTERISTICS

** make a dataset of direct routes and traveltime



u "$data/triplist_na", clear

*** merge ship specifics

merge m:1 ship_id using "$data/ship_all_variables.dta", keepusing(teu dwt)
drop if _merge==2
drop _merge

rename A_* AA_*
rename D_* A_*
merge m:1 A_port_id using "$data/port_cluster_name", update replace
drop if _merge==2
drop _merge 

rename A_* D_*
rename AA_* A_*

merge m:1 A_port_id using "$data/port_cluster_name", update replace
drop if _merge==2
drop _merge 

*drop if dwt<15800
drop if trav==0

drop if A_port_name == D_port_name 

keep if A_co=="US"

collapse (median) travel med_dwt = dwt (count) freq = ship_id (sum) dwt, by(A_port_name D_port_name *country *lat *lon)

geodist A_lat A_lon D_lat D_lon, g(distance)


** merge US district code

merge m:1 A_port_name A_co using "$input/port_list_sample_wUScode", keepusing(US)
drop if _merge!=3
drop _merge

rename D_country iso2
merge m:1 iso2 using "$input/iso2toiso3_uqe", keepusing(iso3)
drop if _merge==2
replace iso3="PRI" if iso2=="PR"
replace iso3="TWN" if iso2=="TW"
drop _merge
rename USdi district

drop iso2

* make a ship and a dwt weigthed avg across multiple ports

bysort distri D_port_name iso3: g N=_N
summ N


foreach var of varlist travel med dista {
g w`var'_shp = `var'*freq
g w`var'_dwt = `var'*dwt
}

collapse (sum) w* freq dwt (mean) N, by(iso3 distric)

foreach var in travel_hs med_dwt distance {
replace w`var'_shp = w`var'_shp/freq
replace w`var'_dwt = w`var'_dwt/dwt
}

save "$data/tt_usports_uq", replace



u "$ps/imp_detl_yearly_116n", clear /* raw data downloaded from http://faculty.som.yale.edu/peterschott/sub_international.htm */
keep if dist_e == dist_u
rename dist_e district

* merge country codes
rename cty_code uscode
merge m:1 uscode using "$ps/ctycodes", keepusing(cty)
drop if cty==""
drop if _merge==2
drop _merge

replace cty="ROU" if cty=="ROM"
drop  uscode 
drop con_*
rename cty iso3

merge m:1 district iso3 using "$data/tt_usports_uq"
keep if _merge==3 /*check if this merge works better if dist_unla is used*/
drop _merge

egen pid = group(commo)
egen cid_i = group(iso3)
egen cid_j = group(district)

g fc=cnt_cha/cnt_val
g fcw=cnt_cha/cnt_wgt


g lnd=ln(wdistance_dwt)
g lnh=ln(wtravel_hs_dwt)
g lndwt=ln(dwt)
g lnmdwt=ln(wmed_dwt_dwt)
g lnship=ln(freq)
g lnadwt=ln(dwt/freq)

*keep if N==1 // keep only the exporters that do not have multiple ports
drop if fc==0
drop if fcw==0

estimates clear
eststo: reghdfe fc lnh if N==1, absorb(pid cid_j) cluster(pid)
eststo: reghdfe fc lnd if N==1, absorb(pid cid_j) cluster(pid) 
eststo: reghdfe fc lnd lnh if N==1, absorb(pid cid_j) cluster(pid)
eststo: reghdfe fc lnd lnh lndwt if N==1, absorb(pid cid_j) cluster(pid)
eststo: reghdfe fc lnd lnh lnadwt lnship if N==1, absorb(pid cid_j) cluster(pid)
eststo: reghdfe fc lnd lnh lnadwt lnship, absorb(pid cid_j) cluster(pid)
esttab,  b(3) ar2 se star( * .1 ** .05 *** .01)

distinct cid_i if _est_est1==1
distinct cid_i if _est_est2==1
distinct cid_i if _est_est3==1
distinct cid_i if _est_est4==1
distinct cid_i if _est_est5==1
distinct cid_i if _est_est6==1

erase "$data/tt_usports_uq.dta"
